

import 'dart:convert';

import 'package:excel_utils/pages/week/bean/DisCustomerRecordBean.dart';
import 'package:excel_utils/pages/week/bean/MonthlyPackageTransferBean.dart';
import 'package:excel_utils/pages/week/week_entry/child/dis_customer_record/dis_customer_record_page.dart';
import 'package:excel_utils/repository/constant.dart';
import 'package:excel_utils/service/DioManager.dart';

abstract class IDisCustomerRecordRepository {

  Future<List<DisCustomerRecordBean>> getDisCustomerRecordData({maps});

  Future<List<DisCustomerRecordBean>> saveDisCustomerRecordData({required List<DisCustomerRecordBean> disCustomerRecordData});

  Future<String> deleteDisCustomerRecordData({required String disCustomerRecordId});

}


class DisCustomerRecordRepository implements IDisCustomerRecordRepository {

  DioManager dioManager = DioManager();

  @override
  Future<String> deleteDisCustomerRecordData({required String disCustomerRecordId}) async {
    final response = await dioManager.get(path: "${Constant.DisCustomerRecordDeleteUrl}?disCustomerRecordId=$disCustomerRecordId");
    dynamic result = json.decode(response.toString());
    if (result["code"] == 0) {
      return result["data"];
    } else {
      return "-1";
    }
  }

  @override
  Future<List<DisCustomerRecordBean>> getDisCustomerRecordData({maps}) async {
    final response = await dioManager.get(path: "${Constant.DisCustomerRecordDataUrl}", queryParameters: maps);
    List<DisCustomerRecordBean> data = [];
    dynamic result = json.decode(response.toString());
    if (result["code"] == 0) {
      result['data'].forEach((v) {
        data.add(DisCustomerRecordBean.fromJson(v));
      });
      return data;
    } else {
      return [];
    }
  }

  @override
  Future<List<DisCustomerRecordBean>> saveDisCustomerRecordData({required List<DisCustomerRecordBean> disCustomerRecordData}) async {
    String jsonData = json.encode(disCustomerRecordData);
    print(jsonData);
    final response = await dioManager.post(path: "${Constant.DisCustomerRecordAddUrl}", data: jsonData);
    List<DisCustomerRecordBean> data = [];
    dynamic result = json.decode(response.toString());
    if (result["code"] == 0) {
      result['data'].forEach((v) {
        data.add(DisCustomerRecordBean.fromJson(v));
      });
      return data;
    } else {
      return [];
    }
  }

}